package com.atguigu.hbase.mr.fruit.hbasetohbase;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName FruitMapper
 * @DeScription FruitMapper
 * @Author 剧情再美终是戏
 * @Date 2019/12/9 9:58
 * @Version 1.0
 **/
public class FruitMapper extends TableMapper<ImmutableBytesWritable, Put> {

    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {

        // 初始化put对象
        Put put = new Put(key.get());

        // 获取所有的单元格
        Cell[] cells = value.rawCells();

        // 过滤只需要info:name这一列
        for (Cell cell : cells) {
            if ("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))
                    && "name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) {
                put.add(cell);
            }
        }
        context.write(key, put);
    }
}
